package com.vietnam.power.controller;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.vietnam.power.domain.MessageInfo;
import com.vietnam.power.service.MailService;
import com.vietnam.power.service.MessageService;

@Controller
public class ContactController {
	@Autowired
	private MessageService messageService;

	@Autowired
	private MessageSource messageSource;

	@RequestMapping(value = { "/sendMessage" }, method = RequestMethod.POST)
	public ModelAndView sendEmail(@ModelAttribute("messageInfo") MessageInfo currentMessage) throws MessagingException {
		String regards = messageSource.getMessage("contact.signature.regards", null, LocaleContextHolder.getLocale());
		currentMessage.setContent(currentMessage.getContent() + "<br/>" + regards + "<br/>" + currentMessage.getName() + "<br/>" + currentMessage.getPhone());

		messageService.createMessage(currentMessage);
		
		return new ModelAndView("redirect:/goContact.html?message=success");
	}
}
